package com.javatiaocao.myblog.controller;


import com.alibaba.fastjson.JSON;
import com.aliyuncs.http.HttpRequest;
import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.model.Categories;
import com.javatiaocao.myblog.service.*;

import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@Slf4j
public class SuperAdminController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    ArticleService articleService;

    @Autowired
    FeedbackService feedbackService;

    @Autowired
    PrivateWordService privateWordService;

    @Autowired
    StatisticsInfoService statisticsInfoService;

    //获取目录分类
    @GetMapping("/getArticleCategories")
    public String getArticleCategories(){
        try {
            DataMap dataMap = categoryService.getCategoriesList();

            return JsonResult.build(dataMap).toJSON();
        }catch (Exception e){
            log.error("SuperAdminController");
        }
        return JsonResult.fail().toJSON();
    }

//    //添加分类
//    @PostMapping("/updateCategory")
//    public String updateCategory(Categories categories,@RequestParam("Type")int type){
//        try {
//            DataMap dataMap = categoryService.addCategory(categories,type);
//            return JsonResult.build(dataMap).toJSON();
//        }catch (Exception e){
//            log.error("addCategory is error"+e);
//        }
//
//        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
//    }

    //下拉列表分类
//    @GetMapping("/findCategoriesName")
//    public String findCategoriesName(){
//        try {
//            DataMap dataMap = categoryService.findCategoriesName();
//            return JsonResult.build(dataMap).toJSON();
//
//        }catch (Exception e){
//            log.error("addCategory is error"+e);
//        }
//        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
//    }

    //文章管理
    @PostMapping("/getArticleManagement")
    public String getArticleManagement(int rows,int pageNum){
        try {
            DataMap dataMap = articleService.selectArticleWithPage(rows,pageNum);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("SuperAdminController is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     *
     * 点赞管理
     *
     * */
    @PostMapping("/getArticleThumbsUp")
    public String getArticleThumbsUp(@AuthenticationPrincipal UserDetails userDetails,int rows, int pageNum){
        try {
            String phone = userDetails.getUsername();
            DataMap dataMap = articleService.getArticleThumbsUp(phone,rows,pageNum);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getArticleThumbsUp is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();

    }

    /**
     *
     *
     * 全部已读
     *
     * **/
    @GetMapping("/readAllThumbsUp")
    public String readAllThumbsUp(@AuthenticationPrincipal UserDetails userDetails){
        try {
            String phone = userDetails.getUsername();
            DataMap dataMap = articleService.readAllThumbsUp(phone);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("readAllThumbsUp is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();

    }

    /**
     *
     *
     * 反馈
     *
     * **/
    @GetMapping("/getAllFeedback")
    public String getAllFeedback(HttpServletRequest request,int rows ,int pageNum){
        try {

            DataMap dataMap = feedbackService.getAllFeedback(rows,pageNum);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getAllFeedback is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();

    }

    /**
     *
     * 悄悄话
     *
     * */
    @PostMapping("/getAllPrivateWord")
    public String getAllPrivateWord(HttpServletRequest request){
        try {

            DataMap dataMap = privateWordService.getAllPrivateWord();
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getAllFeedback is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();

    }

    /**
     *
     *
     * 获取仪表盘信息
     *
     * **/
//    @GetMapping("/getStatisticsInfo")
//    public String getStatisticsInfo(HttpServletRequest request){
//        try {
//
//            DataMap dataMap = statisticsInfoService.getStatisticsInfo();
//            return JsonResult.build(dataMap).toJSON();
//
//        }catch (Exception e){
//            log.error("getStatisticsInfo is error"+e);
//        }
//
//        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
//
//    }


}


